/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import blogLib.itf.IUsuario;
import dao.UsuarioDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Usuario;

/**
 *
 * @author guilherme
 */
public class UsuarioAcoes extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String usuarioBD = System.getProperty("usuarioBD");
            String senhaBD= System.getProperty("senhaBD");
            String instanciaBD = System.getProperty("instanciaDAO");
            String hostBD = System.getProperty("hostBD");
            UsuarioDAO dao = new UsuarioDAO(hostBD, instanciaBD, usuarioBD, senhaBD);
           // UsuarioDAO dao2 = new UsuarioDAO(caminho, nomeBD, usuario, senha)
           
            String action = request.getParameter("action");
            
         //TODO falta implementar o alterar e excluir
            //FIXME Há algum bug maluco que não deixa cadastrar.
       if(action.equals("Cadastrar")){
                    Cadastrar(request, dao, response);
                    
                }else if(action.equals("Alterar")) {
                                        
                }
                else if(action.equals("excluir")) {
                    
                }
            
        } finally {            
            out.close();
        }
    }

    private void Cadastrar(HttpServletRequest request, UsuarioDAO dao,
            HttpServletResponse response) throws SQLException, IOException {
      
        String loginCad = request.getParameter("usuario");
        String senhaCad= request.getParameter("senha");
        String nomeCad= request.getParameter("nome");
       
        //FIXME Verificar pois não está funcionando
        IUsuario u = new Usuario(loginCad, nomeCad, senhaCad);
            
        
        if(Validadores.Usuario(u)) {
            response.sendRedirect("index.jsp?action=Cadastro&Resultado=Erro&usuario="+
                    loginCad+"&nome="+nomeCad);
        }else {
            dao.incluiUsuario(u);
            response.sendRedirect("index.jsp?action=Cadastro&Resultado=Ok&usuario="+
                    loginCad+"&nome="+nomeCad);
        }
    }
    
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UsuarioAcoes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(UsuarioAcoes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(UsuarioAcoes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(UsuarioAcoes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UsuarioAcoes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(UsuarioAcoes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(UsuarioAcoes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(UsuarioAcoes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    
}
